package org.iori.json;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;


public class JSONHelper {
	private static Logger logger = Logger.getLogger(JSONHelper.class);

	public JSONHelper() {
	}

	/**
	* 将对象的json格式数据输出到response
	* 
	* @param response
	* @param object 输出对象
	*/
	public static void printObject(HttpServletResponse response, Object object) {
		try { 
			response.setCharacterEncoding("utf-8");
			response.setContentType("application/json;charset=utf-8");
			PrintWriter writer = response.getWriter();
			writer.print(object);
			writer.flush();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("exception info="+e.getMessage());
			printError(response,new String("json response exception!"));
		}
	}

	/**
	 * json对象输出异常时调用
	 * 
	 * @param response
	 * @param error 错误提示信息
	 */
	public static void printError(HttpServletResponse response,String error) {
		try { 
			response.setCharacterEncoding("utf-8");
			response.setContentType("application/json;charset=utf-8");
			PrintWriter writer = response.getWriter();
			writer.print(error);
			writer.flush();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("exception info="+e.getMessage());
			logger.error("error="+error);
		}
	}

	/**
	 * 将List对象的json格式数据输出到response
	 * 
	 * @param response
	 * @param list
	 */
	@SuppressWarnings("unchecked")
	public static void writeList(HttpServletResponse response, List list) {
		JSONArray object = fromList(list);
		printObject(response, object);
	}

	/**
	 * 将对象的json格式数据输出到response
	 * 
	 * @param response
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	public static void writeObject(HttpServletResponse response, Object object) {
		if (object instanceof JSONObject) {
			writeObject(response, (JSONObject) object);
		} else if (object instanceof JSONArray) {
			writeArray(response, (JSONArray) object);
		} else if (object instanceof List) {
			writeList(response, (List) object);
		} else if (object instanceof String) {
			printObject(response, (String)object);
		} else {
			JSONObject _object = JSONObject.fromObject(object);
			printObject(response, _object);
		}
	}
	 
	/**
	 * 将json格式数据输出到response
	 * 
	 * @param response
	 * @param object
	 */
	public static void writeObject(HttpServletResponse response, JSONObject object) {
		printObject(response, object);
	}

	/**
	 * 将json格式数据输出到response
	 * 
	 * @param response
	 * @param object
	 */
	public static void writeArray(HttpServletResponse response, JSONArray array) {
		printObject(response, array);
	}

	/**
	 * 将List对象转换为json对象
	 * 
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static JSONArray fromList(List list) {
		return JSONArray.fromObject(list);
	}
}
